/*!
    \copyright  Copyright (c) 2008 - 2023 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \version    
    \file       swift_pair.c
    \ingroup    swift_pair
    \brief      Source file for Swift Pair
*/

#ifdef INCLUDE_SWIFT_PAIR
#include "swift_pair.h"
#include "swift_pair_config.h"
#include "swift_pair_advertising.h"
#include <task_list.h>
#include <logging.h>
#include <panic.h>
#include <message.h>

#include "pairing.h"
#ifdef PAUSE_WHEN_NOT_PRIMARY
#include "peer_find_role.h"
#endif

typedef struct
{
    TaskData task;
    swift_pair_config_t config;
}swiftPairTaskData;

swiftPairTaskData swift_pair_task_data;

/*! \brief Handle the Pairing activity messages from pairing module */
static void swiftPair_PairingActivity(PAIRING_ACTIVITY_T *message)
{
    switch(message->status)
    {
        case pairingActivityInProgress:
        {
            DEBUG_LOG("swiftPair_PairingActivity: pairingActivityInProgress");
            SwiftPair_SetDiscoverable(TRUE);
        }
        break;

        case pairingActivityNotInProgress:
        {
            DEBUG_LOG("swiftPair_PairingActivity: pairingActivityNotInProgress");
            SwiftPair_SetDiscoverable(FALSE);
        }
        break;

        default:
            DEBUG_LOG("swiftPair_PairingActivity: Invalid message id ");
        break;
    }

}
/*! \brief Message Handler

    This function is the main message handler for the swift pair module.
*/
static void swiftPair_HandleMessage(Task task, MessageId id, Message message)
{
    UNUSED(task);

    switch (id)
    {
        case PAIRING_ACTIVITY:
            swiftPair_PairingActivity((PAIRING_ACTIVITY_T*)message);
        break;

#ifdef PAUSE_WHEN_NOT_PRIMARY
        case PEER_FIND_ROLE_PRIMARY:
             SwiftPair_PauseAdvertising(FALSE);
        break;

        case PEER_FIND_ROLE_NO_PEER:
        case PEER_FIND_ROLE_SECONDARY:
        case PEER_FIND_ROLE_ACTING_PRIMARY:
             SwiftPair_PauseAdvertising(TRUE);
        break;
#endif

        default:
        break;
    }
}

/*! Get pointer to Swift Pair data structure */
static inline swiftPairTaskData* swiftPair_GetTaskData(void)
{
    return (&swift_pair_task_data);
}

/*! Initialization of swift pair module */
bool SwiftPair_Init(Task init_task)
{
    swiftPairTaskData *theSwiftPair = swiftPair_GetTaskData();
    UNUSED(init_task);
    DEBUG_LOG("SwiftPair_Init");
    memset(theSwiftPair, 0, sizeof(*theSwiftPair));
    theSwiftPair->task.handler = swiftPair_HandleMessage;
    swift_pair_config_t default_config =
    {
        .mode = PAIRING_MODE,
        .device_name_option = DEVICE_NAME_OPTION,
        .advertisment_type = ADVERTISMENT_TYPE,
    };
    theSwiftPair->config = default_config;
    SwiftPair_SetupLeAdvertisingData();
    Pairing_ActivityClientRegister(&(theSwiftPair->task));
#if PAUSE_WHEN_NOT_PRIMARY
    PeerFindRole_RegisterTask(&swift_pair_task_data.task);
#endif
    return TRUE;
}

void SwiftPair_SetConfig(swift_pair_config_t * config)
{
    DEBUG_LOG("SwiftPair_Config mode=0x%x", config->mode);
    swiftPairTaskData *theSwiftPair = swiftPair_GetTaskData();
    theSwiftPair->config = *config;
    SwiftPair_SetupLeAdvertisingData();
}

swift_pair_config_t * SwiftPair_GetConfig(void)
{
    swiftPairTaskData *theSwiftPair = swiftPair_GetTaskData();
    return &theSwiftPair->config;
}

#ifdef HOSTED_TEST_ENVIRONMENT

const TaskData * SwiftPair_GetMessageHandler(void)
{
    return &swift_pair_task_data.task;
}

#endif

#endif /* INCLUDE_SWIFT_PAIR */
